<template>
  <div class="edit">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/admin' }">用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户{{ type }}</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 添加/编辑表单 -->
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      label-width="80px"
      style="margin-top: 20px"
    >
      <el-form-item label="名称" style="width: 400px" prop="title">
        <el-input v-model="form.username"></el-input>
      </el-form-item>
      <el-form-item label="所属角色">
        <el-select v-model="form.roleid" placeholder="请选择所属角色">
          <el-option
            v-for="m in roleList"
            :key="m.id"
            :label="m.rolename"
            :value="m.id"
          >
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="密码" style="width: 400px" prop="title">
        <el-input 
        v-model="form.password" 
        show-password
        placeholder="请输入密码"
        ></el-input>
      </el-form-item>

      <el-form-item label="状态">
        <el-switch
          v-model="form.status"
          :active-value="1"
          :inactive-value="0"
          active-color="green"
          inactive-color="red"
        ></el-switch>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="onSubmit">立即创建</el-button>
        <el-button>取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
// import { mapState } from "vuex";
export default {
  name: "edit",
  data() {
    return {
      type: "添加",
      form: {
        roleid: "",
        username: "",
        password: "",
        status: "",
      },
      roleList:[]
    };
  },
  computed: {
    // ...mapState(["menuList"]),
  },
//   默认执行
  mounted() {
    let uid = this.$route.params.uid;
    console.log(this.$route);
    console.log(this.$route.params);
    console.log("uid:",uid);
    if (uid) {
      this.type = "编辑";
      this.getUserInfo(uid);
    }
    this.getRoleList();
  },
  methods: {
    getRoleList(){
        this.$axios.get("/api/rolelist")
        .then(result=>{
            this.roleList = result.data.list
        }).catch(err=>{
            console.log(err);
        })
    },
    getUserInfo(uid) {
      this.$axios
        .get("/api/userinfo", { params: {uid} })
        .then((result) => {
          this.form = result.data.list;
        })
        .catch((err) => {
          console.log(err);
        });
    },
    onSubmit() {
      //   获得form表单，然后进行验证
      this.$refs["form"].validate((valid) => {
        if (valid) {
          //深拷贝
          let str = JSON.stringify(this.form);
          let data = JSON.parse(str);
          let url = "/api/useradd";
          if (this.type == "编辑") {
            data.uid = this.$route.params.uid;
            url = "/api/useredit";
          }

          this.$axios
            .post(url, data)
            .then((result) => {
              console.log(result);
              if (result.data.code == 200) {
                this.$router.push("/admin");
              }
            })
            .catch((err) => {
              console.log(err);
            });
        } else {
          console.log("error submit");
          return false;
        }
      });
    },
  },
};
</script>

<style>
</style>